package com.dao;

import java.sql.*;


public class PersonnelJdbc {

	  Connection conn;
	  Statement stem;
	  ResultSet rs ;
	  public  void  lianjie (){
		  try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection("jdbc:mysql://120.76.45.216:3306/buy_ticket","root","root");
		} catch (Exception e) {
			e.printStackTrace();
		} 
	  }
	
	  public ResultSet show(){
          this.lianjie();
          // 向数据库发送sql 语句的
		try {
			  stem = conn.createStatement();
			  rs = stem.executeQuery(" select  * from  personnel ");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
          return  rs;
	  }

	public  void  close(){
		try{
			if(conn!=null){
				conn.close();
			}

			if(stem!=null){
				stem.close();
			}

			if(rs!=null){
				rs.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add(String pname, String age, String sex, String pgroup, String pposition, String entry_days) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate("insert into  personnel values (0,'"+pname+"','"+age+"','"+sex+"','"+pgroup+"','"+pposition+"','"+entry_days+"')");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.close();
	}
//
	public void delete(String id) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate("delete from  personnel where pid ='"+id+"'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.close();
	}

	public void update(String pid, String pname, String age, String sex, String pgroup, String pposition, String entry_days) {
		this.lianjie();
		try {
			stem=conn.createStatement();
			stem.executeUpdate("update personnel  set pname='"+pname+"',age='"+age+"',sex='"+sex+"',pgroup='"+pgroup+"',pposition='"+pposition+"',entry_days='"+entry_days+"' where pid='"+pid+"'");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		this.close();
	}
}
